
import * as network_utils from '../../../../../http/NetworkUtils';
import * as constants from '../../../../../common/Constants';
import * as umfutils from '../../../../../utils/UmfUtils';
import * as umfToast from '../../../../../utils/UmfToast';

var pageNo = "1";
var pageSize = "10";
Page({

    data: {
        inRequest: false,//请求中标记
        loadingMore: false,// : false,//请求中标记

        profitDate: '',
        profitType: '',

        detailDataList: []
    },

    onLoad: function (options) {
        pageNo = "1";
        this.setData({
            profitDate: options.date,
            profitType: constants.ProfitTypes[options.profitType]
        });

        // 使用设备可视宽高
        wx.getSystemInfo({
            success: (res) => {
                this.setData({
                    windowHeight: res.windowHeight
                });
            }
        });

        //查询明细数据
        this._requestDetails();
    },

    handleLower: function () {
        if (this.data.inrequest) {
            return;
        }

        pageNo = (parseInt(pageNo) + 1).toString();
        this._requestDetails();
    },

    /**
     * 查询明细数据
     * @private
     */
    _requestDetails() {
        this.setData({
            inRequest: true,
            loadingMore: true,
        });

        let params = {
            date: this.data.profitDate,
            types: this.data.profitType.type,

            pageNum: pageNo,
            pageSize: pageSize
        };

        // let params = {
        //     profitsYearMonth: "201502",
        //     types: "0",
        //     password: "f865b53623b121fd34ee5426c792e5c33af8c227",
        //
        //     pageNum: pageNo,
        //     pageSize: pageSize
        // };

        network_utils.buildRequest(constants.Interfaces.FUNC_S_POS_DT, params, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    let resultList = response["liststr"];
                    this._doHttpSuccess(resultList);
                    return true;
                }
                return false;
            },

            doComplete: () => {
                this.setData({
                    inrequest: false,
                    loadingMore: false,
                });
                return false;
            }
        }, false).send();
    },

    _doHttpSuccess(resultList) {
        if (resultList.length === 0) {
            //没有更多数据
            if (pageNo !== "1") {
                umfToast.showToast("没有更多数据了");
            }
            return;
        }

        this.setData({
            detailDataList: pageNo === "1" ? _getDetailList(resultList) : this.data.detailDataList.concat(_getDetailList(resultList))
        });
    }
});

function _getDetailList(resultList) {
    if (!resultList) {
        resultList = [];
    }

    let helpers = {
        convertList: (item)=> {
            let subArr1 = [],
                subArr2 = [];
            subArr1.push({title: "交易总额(元):", tag: "amount", value: item["amount"]? umfutils.fentoyuan(item["amount"]) : "0.0"});
            subArr1.push({title: "手续费(元):", tag: "agentFee", value: item["agentFee"]? umfutils.fentoyuan(item["agentFee"]) : "0.0" });
            subArr1.push({title: "交易总笔数:", tag: "transNum", value: item["transNum"]? item["transNum"] : "0" });
            subArr1.push({title: "上级代理商:", tag: "upAgentName", value: item["upAgentName"]});
            subArr1.push({title: "归属省份:", tag: "proviceName", value: item["proviceName"]});
            subArr1.push({title: "借记卡费率(%):", tag: "jFeeRate", value: item["jFeeRate"]? item["jFeeRate"] : "0.0"});
            subArr1.push({title: "借记卡封顶(元):", tag: "jFeeMax", value: item["jFeeMax"]? umfutils.fentoyuan(item["jFeeMax"]) : "0.0"});

            subArr2.push({title: "商户号:", tag: "storeId", value: item["merId"]});
            subArr2.push({title: "全额分润金额(元):", tag: "fullProfitsAmt", value: item["fullProfitsAmt"]? umfutils.fentoyuan(item["fullProfitsAmt"]) : "0.0"});
            subArr2.push({title: "实际分润金额(元):", tag: "trueProfitsAmt", value: item["trueProfitsAmt"]? umfutils.fentoyuan(item["trueProfitsAmt"]) : "0.0"});
            subArr2.push({title: "顶级代理商:", tag: "upupAgentName", value: item["upupAgentName"]});
            subArr2.push({title: "商户类别:", tag: "merchantClass", value: item["merchantClass"]});
            subArr2.push({title: "贷记卡费率(%):", tag: "dFeeRate", value: item["dFeeRate"]? item["dFeeRate"] : "0.0"});
            return {title: item["merName"], subArr1: subArr1, subArr2: subArr2};
        }
    };

    let convertList = resultList.map(helpers.convertList);
    return convertList;
}